package com.banciyuan.bcywebview.biz.main.discover.top;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.CoserApi;
import com.banciyuan.bcywebview.api.IllustApi;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.fragment.BaseLazyContainerFragment;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper.OnProgressCallbacks;
import com.banciyuan.bcywebview.base.myinterface.HelpActionInterface;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshListView;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.List;

import de.greenrobot.daoexample.model.DetailComment;


public class TopFragment extends BaseLazyContainerFragment {

    public static final int TAG = 9989;
    public static final int COS_TAG = 12;
    public static final int ILLUST_TAG = 13;
    public static final int WRITER_TAG = 14;

    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;

    private HelpActionInterface mInterface;

    private PullToRefreshListView mRefreshLv;
    private ListView mContentLv;

    private TopUserAdapter mAdapter;
    private TopListHelper mHelper;

    private int topType = TopFragment.COS_TAG;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.top_fragment_layout, container, false);
        initArgs();
        initProgressbar(rootView);
        initUi(rootView);
        initAction();
        return rootView;
    }

    @Override
    public void fetchData() {
        initData();
    }

    @Override
    public void selfUpdateData() {
        mRefreshLv.setRefreshing();
    }

    @Override
    protected void initArgs() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            topType = bundle.getInt(gotoUtil.INTENT_VALUE_ONE, TopFragment.COS_TAG);
        }
        mInterface = new HelpActionInterface() {
            @Override
            public void onNetError(String result) {
                onNetworkError();
            }

            @Override
            public void nodataError(String result) {
                onNodataError();
            }

            @Override
            public void getFrontDataSuccess(List<DetailComment> result) {

            }

            @Override
            public void getLastDataSuccess(String result) {
                lastDataSuccess(result);
            }

            @Override
            public void getFrontDataSuccess(String result) {
                frontDataSuccess(result);
            }
        };
        mHelper = new TopListHelper(getActivity(), mInterface);
    }

    @Override
    protected void initProgressbar(View rootView) {
        mProgressbarView = rootView.findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initUi(View rootView) {
        mRefreshLv = (PullToRefreshListView) rootView.findViewById(R.id.top_refresh_lv);
        mContentLv = mRefreshLv.getRefreshableView();
        mAdapter = new TopUserAdapter(getActivity(), mHelper.getShowList());
        mContentLv.setDividerHeight(UIUtils.dip2px(4, getActivity()));
    }

    @Override
    protected void initAction() {
        mRefreshLv.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                getFrontDataTask();
            }
        });

        mRefreshLv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                getLastDataTask();
            }
        });
        mContentLv.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        getFrontDataTask();
    }

    private void getFrontDataTask() {
        switch (topType) {
            case TopFragment.COS_TAG:
            default:
                mHelper.getFrontListData(CoserApi.getTopcoser());
                break;
            case TopFragment.ILLUST_TAG:
                mHelper.getFrontListData(IllustApi.getTopdrawer());
                break;
            case TopFragment.WRITER_TAG:
                mHelper.getFrontListData(NovelApi.getTopwriter());
                break;
        }
    }

    private void getLastDataTask() {
        switch (topType) {
            case TopFragment.COS_TAG:
            default:
                mHelper.getLastListData(CoserApi.getTopcoser());
                break;

            case TopFragment.ILLUST_TAG:
                mHelper.getLastListData(IllustApi.getTopdrawer());
                break;
            //todo
            case TopFragment.WRITER_TAG:
                mHelper.getLastListData(NovelApi.getTopwriter());
                break;
        }
    }

    public void onNetworkError() {
        mRefreshLv.onRefreshComplete();
        mProgressbarHelper.onFailed();
    }

    public void frontDataSuccess(String result) {
        mAdapter.notifyDataSetChanged();
        mRefreshLv.onRefreshComplete();
        mProgressbarHelper.onSuccess();
    }

    public void lastDataSuccess(String result) {
        mAdapter.notifyDataSetChanged();
        mRefreshLv.onRefreshComplete();
        mProgressbarHelper.onSuccess();
    }

    public void onNodataError() {
        mRefreshLv.onRefreshComplete();
        mProgressbarHelper.onNodata();
    }
}